/*
 * RBFTest.c
 *
 *  Created on: 2023年7月18日
 *      Author: fjx
 */
#if 0
#include "mac/RBFCommon.h"
#include "mac/RBFCommissioning.h"
#include "util.h"
#include "app_log.h"
#include <stdio.h>
#include "mac/MacCrypto.h"
#include "bsp_nvm.h"
#include "device.h"

#define CONFIG_LOG_RBFTEST
RBFMacHeader_t rbf_header_test;

void RBFCommossiong_singal_test (void)
{
  uint8_t buf[10];
  uint8_t packet[255]= {0};
  uint8_t mac[8];
                    //0    1    2    3    4    5    6   7    8    9    10   11   12   13   14    15  16   17
  uint8_t hdr[20] = {0x12,0x34,0x56,0x78,0x90,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x11,0x22,0x33,0x44,0x55,0x66,0x77 };
  uint16_t hdr_len = 16;
  uint8_t  nonce[RBF_NONCE_LENGTH_MAX]={0};
  uint8_t  ad[RBF_AD_LENGTH_MAX]={0};
  SecureElementStatus_t se_ret;


  rbf_header_test.rollungCode = 0x12345678;
  rbf_header_test.frameCtl = 0x99;
  rbf_header_test.syncClock = 0x1122;
  rbf_header_test.hsclock = 0x33;


  l_put_be32(rbf_header_test.rollungCode,buf);//rbf_header_test.buffer);
//  l_put_be8(rbf_header_test.frameCtl,rbf_header_test.buffer+4);

  DS_app_log_hexdump_debug("",buf,5);


  RBF_RET_STATUS ret;

  DS_app_log_hexdump_debug("\nheader data: 0x[\n",hdr,16);


  //memset(nonce,0,sizeof(nonce));

  ret = RBF_payload_crypto_build_nonce (hdr, hdr_len,nonce);
  DS_app_log_debug("get nonce ret = %d ",ret);
  DS_app_log_hexdump_debug("nonce:",nonce,sizeof(nonce));



//  memset(ad,0,sizeof(ad));

  ret = RBF_payload_crypto_build_ad (hdr,hdr_len,ad);
  DS_app_log_debug("get ad ret = %d, ",ret);
  DS_app_log_hexdump_debug("ad",ad,sizeof(ad));


#if 0 //header test
  RBFMacHeader_t header ={
      .rollungCode = 0x12345678,
      .frameCtl = 0x90,
      .syncClock  = 0xaabb,
      .hsclock = 0xcc,
      .panId = 0x11dd22, //3bytes
      .srcId = 0xee,
      .destID = 0xff,
      .routID = 0xa5,
      .cmd = 0x77,

  };

  DS_app_log_debug("===test header start===\n");
  RBF_packet_build_header(header,packet);      //header组包
  DS_app_log_hexdump_debug("===header:0x",packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX);
  RBF_header_parse(packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX, &header);
  DS_app_log_debug("===test header end===\n");
#endif

#if 1
  RBFMacMessageJoinRequest_t join_msg={
      .macAdd2 = 0xabcd,
      .msgSeq = 0x11,
      .msgRetry = 0x22,
      .battV = 0xff,
      .rssi = 0x33,
      .temp = 0x44,
      .qrfR1 = 0x55,
      .qrfR2 = 0x66,
      .echASeq = 0x77,

      .deviceType = 0x88,
      .modelType = 0x99,
      .hardType = 0xaa,
      .macAdd8 = 0x1a2b3c4d5f6e7f,
      .rfVer = 0xabcdef,
      .sVer = 0x56,
      .keyCodeA={0x12,0x34,0x56,0x78,0x90,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x11,0x22,0x33,0x44,0x55},
      .mac = 0xfe112233,
  };
  uint8_t codeA_parse[16];

  DS_app_log_info("===test node paging start===\n");
  RBF_set_node_paging(join_msg,packet);

  //数据加解密
  se_ret = RBF_mac_payload_crypto(packet,12,KEY_ID_CODEA);
  DS_app_log_debug("RBF_mac_payload_crypto ret = %d\n",ret);
  se_ret = RBF_mac_payload_verify(packet,12,KEY_ID_CODEA);
  DS_app_log_debug("RBF_mac_payload_verify ret = %d\n",ret);
  //end of 数据加解密

  RBF_get_node_paging_packet_parse(packet,
                                   RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN,
                                   codeA_parse,
                                   mac);
  DS_app_log_hexdump_debug("====packet:",packet,packet[0]+PACKET_0_OUT_OFFSET);
  DS_app_log_info("===test node paging end===\n");
#endif

//  RBF_header_parse(uint8_t *packet,uint16_t packet_len, RBFMacHeader_t *hdr);

  /*
   * 节点入网确认
   *
   *
   */
#if 1
  RBFMacMessageNodeJoinConfirm_t join_confirm_msg = {
      .macAdd2 = 0xabcd,
      .msgSeq = 0x11,
      .msgRetry = 0x22,
      .battV = 0xff,
      .rssi = 0x33,
      .temp = 0x44,
      .qrfR1 = 0x55,
      .qrfR2 = 0x66,
      .echASeq = 0x77,

      .deviceType = 0x88,
      .modelType = 0x99,
      .hardType = 0xaa,
      .macAdd8 = 0x1a2b3c4d5f6e7f,
      .rfVer = 0xabcdef,
      .sVer = 0x56,
      .mac = 0xfe112233,
  };
  DS_app_log_info("===test join confirm start===\n");
  RBF_set_node_join_confirm(join_confirm_msg,packet);

  //数据加解密
  se_ret = RBF_mac_payload_crypto(packet,12,KEY_ID_CODEB);
  DS_app_log_debug("RBF_mac_payload_crypto ret = %d\n",ret);
  se_ret = RBF_mac_payload_verify(packet,12,KEY_ID_CODEB);
  DS_app_log_debug("RBF_mac_payload_verify ret = %d\n",ret);
  //end of 数据加解密

  RBF_get_node_join_confirm(packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_CONFIRM_LEN);
  DS_app_log_hexdump_debug("====packet:0x[",packet,packet[0]+PACKET_0_OUT_OFFSET);
  DS_app_log_info("===test join confirm end===\n");
#endif

#if 1

  RBFMacMessageGWAlloawJoinNetwork_t gw_join_msg ={
      .macAdd4 = 0x11223344,
      .rollingCode = 0x55667788,
      .systemID = 0xaabbcc,  //3Bytes
      .localID = 0xddee,
      .broadcastKeyCode = {0x12,0x34,0x56,0x78,0x90,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x11,0x22,0x33,0x44,0x55},
      .keyCodeB = {0xBB,0xB4,0xB6,0xB8,0xB0,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x11,0x22,0x33,0x44,0x55},
      .systemKeyCode = {0xCC,0xC4,0xC6,0xC8,0x90,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x11,0x22,0x33,0x44,0x55},
      .mac = 0xa55ab66b,
  };
//  uint8_t ret = 0;
  DS_app_log_info("===test gw allow join start===\n");
  RBF_set_gw_allow_join_net(gw_join_msg,packet);

  //数据加解密
  se_ret = RBF_mac_payload_crypto(packet,12,KEY_ID_CODEAB);
  DS_app_log_debug("RBF_mac_payload_crypto ret = %d\n",ret);
  se_ret = RBF_mac_payload_verify(packet,12,KEY_ID_CODEAB);
  DS_app_log_debug("RBF_mac_payload_verify ret = %d\n",ret);
  //end of 数据加解密

  RBF_get_gw_allow_join_net(packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NETWORK_LEN);


  DS_app_log_hexdump_debug("====packet:0x[",packet,packet[0]+PACKET_0_OUT_OFFSET);
  DS_app_log_debug("===test gw allow join net end===\n");
#endif
  DS_app_log_info("[Payload] GW ALLOW JOIN=%d,%d,%d\n",
         RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NET_INDEX_FIRST,
         RBF_MAC_PAYLOAD_JOIN_REQ_INDEX_FIRST,
         RBF_MAC_PAYLOAD_JOIN_CONFIRM_INDEX_FIRST
         );

//
//  uint8_t x[20],y[20]={0};
//  memset(x,0,20);
//  RBF_get_data_defaultKey1(x);
//  DS_app_log_hexdump_debug("default key1 :",x,16);
#if 0
  RBF_keyNVM_test();
#endif


}


//=====
uint8_t _key[RBF_KEY_LENGTH_MAX] ={0};
uint8_t _keyA[RBF_KEY_LENGTH_MAX] =KEY_CODEA_DEFAULT;
uint8_t _keyB[RBF_KEY_LENGTH_MAX] =KEY_CODEB_DEFAULT;
uint8_t _keyAB[RBF_KEY_LENGTH_MAX] = {0};

uint8_t _keydefault1[RBF_KEY_LENGTH_MAX] =DEFAULTKEY_1_VALUE_DEFALUT;
uint8_t _keydefault2[RBF_KEY_LENGTH_MAX] =DEFAULTKEY_2_VALUE_DEFALUT;

uint8_t _keysystem[RBF_KEY_LENGTH_MAX]=KEY_SYSTEM_DEFAULT;
uint8_t _keybroadcast[RBF_KEY_LENGTH_MAX]=KEY_BROADCAST_DEFAULT;

void RBFCommission_KeyInit (void)
{//1.赋值key到NVM
  RBF_set_data_codeA(_keyA);
  RBF_set_data_codeB(_keyB);
  RBF_set_data_defaultKey1(_keydefault1);
  RBF_set_data_defaultKey2(_keydefault2);

  //HUB端
  RBF_set_data_systemKeyCode(_keysystem);
  RBF_set_data_broadcastKeyCode(_keybroadcast);


  //2.从NVM取key,并得到key_id
  RBF_get_data_defaultKey1(_key);
  RBF_set_keyid_defaultKey1(_key);
//  RBF_get_data_defaultKey2(_key);
//  RBF_set_keyid_defaultKey2(_key);

  //RBM
  RBF_get_data_codeA(_key);
  RBF_set_keyid_codeA(_key);
  //HUB
  RBF_get_data_codeB(_key);
  RBF_set_keyid_codeB(_key);

  RBF_key_group_info_log();

}
void RBFCommissiong_join_node_paging (void)
{
  SecureElementStatus_t se_ret;
  RBF_RET_STATUS ret;
  uint8_t packet[255]= {0};
  uint8_t mac[8];


  RBFMacMessageJoinRequest_t join_msg={
      .macAdd2 = 0xabcd,
      .msgSeq = 0x11,
      .msgRetry = 0x22,
      .battV = 0xff,
      .rssi = 0x33,
      .temp = 0x44,
      .qrfR1 = 0x55,
      .qrfR2 = 0x66,
      .echASeq = 0x77,

      .deviceType = 0x88,
      .modelType = 0x99,
      .hardType = 0xaa,
      .macAdd8 = 0x1a2b3c4d5f6e7f,
      .rfVer = 0xabcdef,
      .sVer = 0x56,
      .keyCodeA={0},
      .mac = 0xfe112233,
  };
  uint8_t codeA_parse[16];

  RBF_get_data_codeA(_key);  //获取keyCodeA
  memcpy(join_msg.keyCodeA,_key,RBF_KEY_LENGTH_MAX);

  DS_app_log_info("===test node paging start===\n");
  RBF_set_node_paging(join_msg,packet);

  //数据加解密
  se_ret = RBF_mac_payload_crypto(packet,16,KEY_ID_DEFAULT_KEY1);
  DS_app_log_debug("RBF_mac_payload_crypto se_ret = %d\n",se_ret);
  se_ret = RBF_mac_payload_verify(packet,16,KEY_ID_DEFAULT_KEY1);
  DS_app_log_debug("RBF_mac_payload_verify se_ret = %d\n",se_ret);
  //end of 数据加解密

  ret = RBF_get_node_paging_packet_parse(packet,
                                   RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_REQ_LEN,
                                   codeA_parse,
                                   mac);

  DS_app_log_hexdump_debug("====packet:0x",packet,packet[0]+PACKET_0_OUT_OFFSET);
  if(ret == 0 ){
      DS_app_log_info("codeAkey %s \n\n\n",(memcmp(_keyA,codeA_parse,RBF_KEY_LENGTH_MAX) == 0 ?"same":"diff"));

      DS_app_log_hexdump_debug("codeAkey: ",_keyA,RBF_KEY_LENGTH_MAX);
      DS_app_log_hexdump_debug("codeAkey: ",codeA_parse,RBF_KEY_LENGTH_MAX);
  }


}
void RBFCommissiong_gw_allow_join_net  (void)
{
  SecureElementStatus_t se_ret;
  RBF_RET_STATUS ret;
  uint8_t packet[255]= {0};
  uint8_t len;

  RBFMacMessageGWAlloawJoinNetwork_t gw_join_msg ={
      .macAdd4 = 0x11223344,
      .rollingCode = 0x55667788,
      .systemID = 0xaabbcc,  //3Bytes
      .localID = 0xddee,
      .broadcastKeyCode = {0},
      .keyCodeB = {0},
      .systemKeyCode = {0},//
      .mac = 0xa55ab66b,
  };
  /*
   * 从NVM获取CodeB,broadcastKeyCode,systemKeyCode
   */

  RBF_get_data_codeB(_key);  //获取keyCodeB
  memcpy(gw_join_msg.keyCodeB,_key,RBF_KEY_LENGTH_MAX);
  RBF_get_data_systemKeyCode(_key);//获取
  memcpy(gw_join_msg.systemKeyCode,_key,RBF_KEY_LENGTH_MAX);
  RBF_get_data_broadcastKeyCode(_key);
  memcpy(gw_join_msg.broadcastKeyCode,_key,RBF_KEY_LENGTH_MAX);

  /* 1. HUB 先保存ABke
   * 2. systemKeyCode进行AES(codeAB)加密
   */
  //1.保存CodeAB
  RBF_get_data_codeA(_key);
  XorDataLine(_key,gw_join_msg.keyCodeB,RBF_KEY_LENGTH_MAX);
  RBF_set_data_codeAB(_key);  //保存
  //set codeAB key_id
  RBF_get_data_codeAB(_key);
  RBF_set_keyid_codeAB(_key);
  RBF_key_group_info_log();
  //2.systemKeyCode(AES)
  RBF_aes_ae(KEY_ID_CODEAB,gw_join_msg.systemKeyCode,RBF_KEY_LENGTH_MAX,_key,RBF_KEY_LENGTH_MAX,&len);
  memcpy(gw_join_msg.systemKeyCode,_key,RBF_KEY_LENGTH_MAX);
  RBF_aes_ae(KEY_ID_CODEAB,gw_join_msg.broadcastKeyCode,RBF_KEY_LENGTH_MAX,_key,RBF_KEY_LENGTH_MAX,&len);
  memcpy(gw_join_msg.broadcastKeyCode,_key,RBF_KEY_LENGTH_MAX);

  DS_app_log_info("===test gw allow join start===\n");
  RBF_set_gw_allow_join_net(gw_join_msg,packet);



  //payload数据加解密
  se_ret = RBF_mac_payload_crypto(packet,16,KEY_ID_DEFAULT_KEY1);
  DS_app_log_debug("RBF_mac_payload_crypto se_ret = %d\n",se_ret);
  se_ret = RBF_mac_payload_verify(packet,16,KEY_ID_DEFAULT_KEY1);
  DS_app_log_debug("RBF_mac_payload_verify se_ret = %d\n",se_ret);
  //end of 数据加解密

  ret = RBF_get_gw_allow_join_net(packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_GW_ALLOW_JOIN_NETWORK_LEN);

  if(ret == 0 ){
      //获取codeB,codeAB,systemKey
      RBFMacMessageGWAlloawJoinNetwork_t msg;



      RBF_gw_allow_join_net_payload_packetToMsg(packet,&msg);
      RBF_get_data_codeB(_key);
      DS_app_log_info("codeBkey %s \n\n\n",(memcmp(_key,msg.keyCodeB,RBF_KEY_LENGTH_MAX) == 0 ?"same":"diff"));
      RBF_get_data_broadcastKeyCode(_key);
      DS_app_log_info("broadcastKeyCode %s \n\n\n",(memcmp(_key,msg.broadcastKeyCode,RBF_KEY_LENGTH_MAX) == 0 ?"same":"diff"));
      RBF_get_data_systemKeyCode(_key);
      DS_app_log_info("systemKey %s \n\n\n",(memcmp(_key,msg.systemKeyCode,RBF_KEY_LENGTH_MAX) == 0 ?"same":"diff"));

      //aes 解密
      RBF_aes_de(KEY_ID_CODEAB,gw_join_msg.systemKeyCode,RBF_KEY_LENGTH_MAX,_key,RBF_KEY_LENGTH_MAX,&len);
//      DS_app_log_hexdump_debug("***system code(cipher) =0x",gw_join_msg.systemKeyCode,RBF_KEY_LENGTH_MAX);
//      DS_app_log_hexdump_debug("***system code(plain) =0x",_key,RBF_KEY_LENGTH_MAX);
      DS_app_log_hexdump_debug("***system code(cipher)",gw_join_msg.systemKeyCode,RBF_KEY_LENGTH_MAX);
      DS_app_log_hexdump_debug("***system code(plain)",_key,RBF_KEY_LENGTH_MAX);

      memcpy(_key,gw_join_msg.systemKeyCode,RBF_KEY_LENGTH_MAX);

      RBF_aes_de(KEY_ID_CODEAB,gw_join_msg.broadcastKeyCode,RBF_KEY_LENGTH_MAX,_key,RBF_KEY_LENGTH_MAX,&len);
      DS_app_log_hexdump_debug("***broadcast code(cipher)",gw_join_msg.broadcastKeyCode,RBF_KEY_LENGTH_MAX);
      DS_app_log_hexdump_debug("***broadcast code(plain)",_key,RBF_KEY_LENGTH_MAX);

      memcpy(_key,gw_join_msg.broadcastKeyCode,RBF_KEY_LENGTH_MAX);

      {
//      //RFM保存CodeAB
//      RBF_get_data_codeA(_key);
//      XorDataLine(_key,msg.keyCodeB,RBF_KEY_LENGTH_MAX);
//      RBF_set_data_codeAB(_key);  //保存
//
//      //set codeAB key_id
//      RBF_get_data_codeAB(_key);
//      RBF_set_keyid_codeAB(_key);
//      RBF_key_group_info_log();
      }


  }
  DS_app_log_hexdump_debug("====packet:0x",packet,packet[0]+PACKET_0_OUT_OFFSET);
  DS_app_log_info("===test gw allow join net end===\n");

}

void RBFCommissiong_join_confirm (void)
{
  SecureElementStatus_t se_ret;
  RBF_RET_STATUS ret;
  uint8_t packet[255]= {0};

  RBFMacMessageNodeJoinConfirm_t join_confirm_msg = {
      .macAdd2 = 0xabcd,
      .msgSeq = 0x11,
      .msgRetry = 0x22,
      .battV = 0xff,
      .rssi = 0x33,
      .temp = 0x44,
      .qrfR1 = 0x55,
      .qrfR2 = 0x66,
      .echASeq = 0x77,

      .deviceType = 0x88,
      .modelType = 0x99,
      .hardType = 0xaa,
      .macAdd8 = 0x1a2b3c4d5f6e7f,
      .rfVer = 0xabcdef,
      .sVer = 0x56,
      .mac = 0xfe112233,
  };
  DS_app_log_info("===test join confirm start===\n");
  RBF_set_node_join_confirm(join_confirm_msg,packet);

  //数据加解密
  se_ret = RBF_mac_payload_crypto(packet,12,KEY_ID_CODEAB);
  DS_app_log_debug("RBF_mac_payload_crypto ret = %d\n",se_ret);
  se_ret = RBF_mac_payload_verify(packet,12,KEY_ID_CODEAB);
  DS_app_log_debug("RBF_mac_payload_verify ret = %d\n",se_ret);
  //end of 数据加解密

  RBF_get_node_join_confirm(packet,RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBF_MAC_PAYLOAD_JOIN_CONFIRM_LEN);
  DS_app_log_hexdump_debug("====packet:0x[",packet,packet[0]+PACKET_0_OUT_OFFSET);
  DS_app_log_info("===test join confirm end===\n");
}





void RBFCommissiong_t2 (void)
{
  //
  uint8_t __key[RBF_KEY_LENGTH_MAX] = KEY_NVM_DEFAULT;
  uint8_t __key_de[32]={0};
  uint8_t plaintext[32]={0};
  size_t key_len = 0,plain_len=0;
  SecureElementStatus_t ret = 0;

  DS_nvmInit();
  RBFCommission_KeyInit();


  RBFCommissiong_join_node_paging();
  RBFCommissiong_gw_allow_join_net();

  RBFCommissiong_join_confirm();
#if 1
//  DS_aes_get_key_id();
//  RBF_get_data_codeAB(_key);  //获取codeAB
//  DS_aes_init(_key,RBF_KEY_LENGTH_MAX);

  ret = RBF_aes_ae(KEY_ID_CODEAB,__key,RBF_KEY_LENGTH_MAX,__key_de,sizeof(__key_de),&key_len);

  DS_app_log_hexdump_debug("aes ae: ",__key_de,20);
//  __key_de[16] += 1;
//  __key_de[17] +=2;
  ret = RBF_aes_de(KEY_ID_CODEAB,__key_de,key_len,plaintext,sizeof(plaintext),&plain_len);
  DS_app_log_hexdump_debug("aes de: ",plaintext,20);

  DS_app_log_debug("key is =%s\n",(memcmp(__key,plaintext,RBF_KEY_LENGTH_MAX)==0 ? "same":"diff"));
#endif

  ret = RBF_aes_ae(KEY_ID_CODEAB,__key,RBF_KEY_LENGTH_MAX,__key_de,sizeof(__key_de),&key_len);

  DS_app_log_hexdump_debug("aes ae: ",__key_de,20);
//  __key_de[16] += 1;
//  __key_de[17] +=2;
  ret = RBF_aes_de(KEY_ID_CODEAB,__key_de,key_len,plaintext,sizeof(plaintext),&plain_len);
  DS_app_log_hexdump_debug("aes de: ",plaintext,20);

  DS_app_log_info("key is =%s\n",(memcmp(__key,plaintext,RBF_KEY_LENGTH_MAX)==0 ? "same":"diff"));
//  ecb_test();
}

void RBFCommissiong_test (void)
{
  uint8_t key[16]={0};
  uint8_t buf[255]={0};
//  RBFCommissiong_test();

  RBFCommissiong_t2();
//  DS_app_log_hexdump_debug("CodeA key:",key,16);
//  get_key_from_nvm(KEY_ID_CODEA,key);
//  DS_app_log_hexdump_debug("CodeA key",key,16);
//  key[1]+=1;
//  key[2]+=2;
//  set_key_from_nvm(KEY_ID_CODEA,key);
//  DS_app_log_hexdump_debug("CodeA key",key,16);
//  get_key_from_nvm(KEY_ID_CODEA,key);,
//  DS_app_log_hexdump_debug("CodeA key",key,16);

  getThisParam(ENbyNodeJoinInfo,buf);
  DS_app_log_hexdump_debug("nodeJoinInfo:",buf,NODE_JOIN_INFO_LENGTH);

  DS_app_log_info("===test end===\n");
  DS_app_log_debug("===debug end===\n");
  DS_app_log_error("===debug end===\n");

}

#endif
